/**
 * 
 */
package manet.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableRowSorter;

import manet.Node;

/**
 * @author stra0556 grin0077
 *
 */
public class InfoPanel extends JPanel {

	/**
	 * Serial ID
	 */
	private static final long serialVersionUID = 1432986590834L;

	/** The information table for the panel */
	private InfoTable nodeTable = new InfoTable();


	private Object dataLock = new Object();
	/**
	 * The default constructor for the information panel that creates
	 * a simple panel for the group information 
	 */
	public InfoPanel(){
		super(new GridLayout(1,0));

		/* Create a new JTable from the panel */
		TableRowSorter<InfoTable> sorter = new TableRowSorter<InfoTable>(nodeTable);
		JTable table = new JTable( nodeTable );
		table.setRowSorter(sorter);
		table.setPreferredScrollableViewportSize(new Dimension(500, 100));
		table.setFillsViewportHeight(true);
		table.setDefaultRenderer(Color.class, new ColorRenderer(true));

		//Create the scroll pane and add the table to it.
		JScrollPane scrollPane = new JScrollPane(table);

		//Add the scroll pane to this panel.
		add(scrollPane);

		/* Create the new frame */
		JFrame frame = new JFrame("Robot Information");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Create and set up the content pane.
		this.setOpaque(true);
		frame.setContentPane(this);

		//Display the window.
		frame.pack();
		frame.setVisible(true);

	}

	public boolean isCellEditable(int row, int col) {
		//Note that the data/cell address is constant,
		//no matter where the cell appears onscreen.
		if( row == 0){
			return true;
		}
		return false;
	}

	/**
	 * @param nodes The list of nodes that will be used to populate the table 
	 */
	public void updateTable(List<Node> nodes){
		nodeTable.fillTable( nodes );
	}

	private class InfoTable extends AbstractTableModel {

		/**
		 * Typical serial number 
		 */
		private static final long serialVersionUID = 6944978329159399956L;

		/** The column names to be displayed */
		private final String[] COLUMN_NAMES = {"Group ID",
				"Group Color",
				"Key Value",
				"Host Name",
		"Port Number"};

		/** 2-D array to hold the data information */
		private Object[][] data = {{"","","","",""}};

		/** Get the number of columns */
		public int getColumnCount() {
			return COLUMN_NAMES.length;
		}

		/** Get the number of rows */
		public int getRowCount() {
			synchronized (dataLock){
				return data.length;
			}
		}

		/** Get the names of the columns */
		public String getColumnName(int col) {
			return COLUMN_NAMES[col];
		}

		/** Get the values at a certain place */
		public Object getValueAt(int row, int col) {
			synchronized (dataLock){
				return data[row][col];
			}
		}

		/** Get the class of a certain column */
		public Class<?> getColumnClass(int c) {
			Class<?> classType = getValueAt(0, c) == null ? dataLock.getClass() : getValueAt(0, c).getClass();
			return classType;

		}

		/** Set the value at a specific place */
		public void setValueAt(Object value, int row, int col) {
			synchronized (dataLock){
				data[row][col] = value;
			}
		}


		/**
		 * @param nodes The information to be displayed 
		 */
		private  void fillTable(List<Node> nodes){
			synchronized(dataLock){
				/* Create the correct sized table */
				data= new Object[nodes.size()][COLUMN_NAMES.length];

				/* Populate the data with the corresponding objects */
				for( int i = 0;  i < nodes.size(); ++i ){
					Node node = nodes.get(i);
					data[i][0] = (Object)new Integer( node.getGroupID() );
					data[i][1] = (Object)node.getFillColor();
					data[i][2] = (Object)node.getKey().toString(16);
					data[i][3] = (Object)node.getServer();
					data[i][4] = (Object)new Integer( node.getPort() );
				}

				/* Let them know the table changed */
				fireTableDataChanged();
			}
			return;
		}

	}
}
